package com.xhwl.centiotdata.utils;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.io.Serializable;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author Leon
 * @Description: eesd  线程池工具
 * @Date 2021/5/15 10:08
 */
public class ThreadPoolExecutorService implements Serializable {

    private static ThreadPoolExecutor threadPool;

    static {
        ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("eesd-pool-%d").build();
        threadPool = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), Runtime.getRuntime().availableProcessors() * 2, 60, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>(1000), threadFactory, new ThreadPoolExecutor.AbortPolicy());
    }

    private ThreadPoolExecutorService() {
        if (EnumHolder.INSTANCE != null) {
            throw new RuntimeException("=================> 非法操作! <==================");
        }
    }

    private enum EnumHolder {
        INSTANCE;
        private ThreadPoolExecutorService instance;

        EnumHolder() {
            instance = new ThreadPoolExecutorService();
        }
    }

    public static ThreadPoolExecutorService getInstance() {
        return EnumHolder.INSTANCE.instance;
    }

    private Object readResolve() {
        return EnumHolder.INSTANCE.instance;
    }

    public static ThreadPoolExecutor getThreadPool() {
        return threadPool;
    }
}
